/**
********************************************************************************
* @file     main.c
* @brief    对文件的简要说明
* @details  对文件的详细说明
* @author   jcai
* @date     2024-06-05 14:09:36
* @copyright Copyright &copy; 2024 wy1107412334@163.com All rights reserved
* @version  v0.0.1
*
* \n Email:  wy1107412334@163.com
* \n Blog:   None
* \n Github: https://github.com/wy1107412334
* \n Gitee:  https://gitee.com/git.git1107412334.com
*
********************************************************************************
* @note
* None.
********************************************************************************
* @attention
* None.
********************************************************************************
@verbatim
@endverbatim
********************************************************************************
* @par 历史记录
* jcai, 2024-06-05 14:09:36
* - 首次编写
*
*<center>Copyright &copy; 2024 wy1107412334@163.com All rights reserved</center>
********************************************************************************
*/

/* 头文件导入区 ------------------------------------------------------------- */

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>


/** @defgroup MAIN MAIN
* @brief main modules
* @{
*/



/* 私有宏定义区 ------------------------------------------------------------- */

#define WIN_X_POS                               0
#define WIN_Y_POS                               0
#define WIN_WIDTH                               640
#define WIN_HIGHT                               480

#define ARRAY_LENGTH(arr)                       (sizeof(arr) / sizeof(arr[0]))


/* 私有数据类型声明区 ------------------------------------------------------- */








/* 私有变量定义区 ----------------------------------------------------------- */





/** @defgroup MAIN_Exported_Variables main exported variables
* @brief 全局变量定义
* @{
*/



/** defgroup MAIN_Exported_Variables
* @}
*/





/* 私有函数定义区 ----------------------------------------------------------- */

static void outSDLError(const char * msg)
{
    fprintf(stderr, "%s error: %s\n", msg, SDL_GetError());
}

static void draw_graphics(SDL_Renderer * sdlrd)
{
    // 備份畫筆顏色
    Uint8 rgba[4];
    SDL_GetRenderDrawColor(sdlrd, &rgba[0], &rgba[1], &rgba[2], &rgba[3]);

    // 設置畫筆顏色
    SDL_SetRenderDrawColor(sdlrd, 255, 0, 0, rgba[3]);

    // 畫點
    SDL_RenderDrawPoint(sdlrd, 20, 20);

    // 畫點的集合
    SDL_Point points[] = {
        { .x = 1, .y = 1 },
        { .x = 3, .y = 3 },
        { .x = 5, .y = 2 },
        { .x = 7, .y = 3 },
        { .x = 9, .y = 4 },
    };
    SDL_RenderDrawPoints(sdlrd, points, ARRAY_LENGTH(points));

    // 畫線條
    SDL_RenderDrawLine(sdlrd, 50, 50, 100, 100);

    // 畫折線。畫了一個四邊形
    SDL_Point lpoints[] = {
        { .x = 20, .y = 30 },
        { .x = 20, .y = 40 },
        { .x = 40, .y = 45 },
        { .x = 40, .y = 20 },
        { .x = 20, .y = 30 },
    };
    SDL_RenderDrawLines(sdlrd, lpoints, ARRAY_LENGTH(lpoints));

    // 畫一個矩形
    SDL_Rect rect = { .x = 20, .y = 90, .w = 10, .h = 20 };
    SDL_RenderDrawRect(sdlrd, &rect);

    // 畫一組矩形
    SDL_Rect rects[] = {
        { .x = 90, .y = 20, .w = 5, .h = 10 },
        { .x = 100, .y = 25, .w = 5, .h = 10 },
        { .x = 120, .y = 30, .w = 15, .h = 10 },
    };
    SDL_RenderDrawRects(sdlrd, &rects[0], ARRAY_LENGTH(rects));

    // 畫一個填充顏色的矩形
    SDL_Rect rect2 = { .x = 150, .y = 50, .w = 17, .h = 31 };
    SDL_RenderFillRect(sdlrd, &rect2);

    // 畫一組填充顏色的矩形
    SDL_Rect rects2[] = {
        { .x = 180, .y = 20, .w = 5, .h = 10 },
        { .x = 230, .y = 25, .w = 5, .h = 10 },
        { .x = 270, .y = 30, .w = 15, .h = 10 },
    };
    SDL_RenderFillRects(sdlrd, &rects2[0], ARRAY_LENGTH(rects2));

    // 恢復畫筆顏色
    SDL_SetRenderDrawColor(sdlrd, rgba[0], rgba[1], rgba[2], rgba[3]);
}








/** @defgroup MAIN_Exported_Functions main exported functions
* @brief 全局函数定义
* @{
*/



/**
********************************************************************************
* @brief    简要说明
* @details  详细说明
* @param    none
* @param[in]     a: 该参数用于输入
* @param[in,out] b: 该参数用于输入和输出
* @param[out]    c: 该参数用于输出
* @retval  none
* @retval  0 函数成功
* @retval  1 函数失败
* @return  返回0成功；否则失败
* @note    none
*/
int main(int argc, char *argv[])
{
    (void) argc;
    (void) argv;

    if (SDL_Init(SDL_INIT_VIDEO) != 0)
    {
        outSDLError("SDL_Init()");
        goto initRet;
    }

    SDL_Window * sdlwin = SDL_CreateWindow("title", WIN_X_POS, WIN_Y_POS, WIN_WIDTH, WIN_HIGHT, SDL_WINDOW_SHOWN);
    if (sdlwin == NULL)
    {
        outSDLError("SDL_CreateWindow()");
        goto winRet;
    }

    SDL_Renderer * sdlrd = SDL_CreateRenderer(sdlwin, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    if (sdlrd == NULL)
    {
        outSDLError("SDL_CreateRenderer()");
        goto renderRet;
    }

    int quit = 0;
    SDL_Event e;
    while (!quit)
    {
        while (SDL_PollEvent(&e))
        {
            switch (e.type)
            {
            case SDL_QUIT:
                quit = 1;
                break;

            default:
                break;
            }
        }

        SDL_RenderClear(sdlrd);

        draw_graphics(sdlrd);

        SDL_RenderPresent(sdlrd);

        SDL_Delay(40);
    }

    SDL_DestroyRenderer(sdlrd);
    SDL_DestroyWindow(sdlwin);
    SDL_Quit();

    return EXIT_SUCCESS;

renderRet:
    SDL_DestroyWindow(sdlwin);
winRet:
    SDL_Quit();
initRet:
    return EXIT_FAILURE;
}


/** defgroup MAIN_Exported_Functions
* @}
*/

/** defgroup MAIN
* @}
*/


/*********************************** END OF FILE ******************************/
